//
//  ArrayImView.swift
//  DataStructure
//
//  Created by kys-20 on 2019/8/27.
//  Copyright © 2019 kys-20. All rights reserved.
//

import Foundation
import UIKit
class ArrayImView: UIView {
    let textField = UITextField()//输入框
    let buttonEnqueue = UIButton()//入列按钮
    let buttonDequeue = UIButton()//出列按钮
    let buttonClearQueue = UIButton()//清除队列按钮
    let labelCircle = UILabel()//动画过程的蓝圈
    let labelMark = UILabel()//用来标识是进队出对
    let labelValue = UILabel()//用来展示进队出对的值
    let slider = UISlider()//滑块 用来控制时间
    
    var labelArrayMark = [UILabel]()//标记队列头尾坐标
    var labelArraySubscriptMark = [UILabel]()//队列label数组的下标
    var labelArrayQueue = [UILabel]()//队列label数组
    
    var headOfTheQueue = 0//队首下标
    var tailOfTheQueue = 0//队尾下标
    //    var countQueue = 0//总计变量
    var sumTime : Double = 2.00 //总时间
    var time:Timer!
    var timeNo = 0//计时器次数
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    
    //创建初始视图-----------------------------------------------------------------
    func setUpUI(){
        print(self.bounds.size.height)
        slider.frame = CGRect(x: 10, y: UIScreen.main.bounds.height-200, width: UIScreen.main.bounds.width-20, height: 30)
        slider.minimumValue = 0
        slider.maximumValue = 4
        slider.value = 2.00
        self.addSubview(slider)
        slider.isContinuous = false//滑块滑动停止后才触发ValueChanged事件
        slider.addTarget(self, action: #selector(ActionSliderValueChange), for: .valueChanged)
        
        textField.frame = CGRect(x: 20, y: 35, width: 70, height: 40)
        textField.borderStyle = .roundedRect
        textField.layer.borderColor = UIColor.red.cgColor
        textField.layer.borderWidth = 1
        self.addSubview(textField)
        buttonEnqueue.frame = CGRect(x: 100, y: 35, width: 70, height: 40)
        buttonEnqueue.setTitle("Enqueue", for: .normal)
        buttonEnqueue.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        buttonEnqueue.setTitleColor(UIColor.black, for: .normal)
        buttonEnqueue.backgroundColor = UIColor.white
        buttonEnqueue.layer.borderWidth = 1
        self.addSubview(buttonEnqueue)
        buttonEnqueue.addTarget(self, action: #selector(ActionButtonEnqueue), for: .touchUpInside)
        
        buttonDequeue.frame = CGRect(x: 180, y: 35, width: 70, height: 40)
        buttonDequeue.setTitle("Dequeue", for: .normal)
        buttonDequeue.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        buttonDequeue.setTitleColor(UIColor.black, for: .normal)
        buttonDequeue.backgroundColor = UIColor.white
        buttonDequeue.layer.borderWidth = 1
        buttonDequeue.addTarget(self, action: #selector(ActionDequeue), for: .touchUpInside)
        self.addSubview(buttonDequeue)
        
        buttonClearQueue.frame = CGRect(x: 260, y: 35, width: 100, height: 40)
        buttonClearQueue.setTitle("Clear Queue", for: .normal)
        buttonClearQueue.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        buttonClearQueue.setTitleColor(UIColor.black, for: .normal)
        buttonClearQueue.backgroundColor = UIColor.white
        buttonClearQueue.layer.borderWidth = 1
        buttonClearQueue.addTarget(self, action: #selector(ActionClearQueue), for: .touchUpInside)
        self.addSubview(buttonClearQueue)
        
        labelMark.frame = CGRect(x: 20, y: 90, width: 120, height: 20)
        labelMark.font = UIFont.systemFont(ofSize: 15)
        labelMark.textColor = UIColor.black
        labelMark.textAlignment = .center
        labelMark.isHidden = true
        self.addSubview(labelMark)
        
        labelValue.frame = CGRect(x: 130, y: 90, width: 80, height: 20)
        labelValue.textColor = UIColor.black
        labelValue.textAlignment = .center
        labelValue.isHidden = true
        self.addSubview(labelValue)
        
        let lableQueueWidth = (UIScreen.main.bounds.width-40)/7
        for i in 0...3{
            let label = UILabel()
            label.frame = CGRect(x: 20+CGFloat(i*60), y: 150, width: CGFloat(lableQueueWidth), height: CGFloat(lableQueueWidth))
            label.textAlignment = .center
            label.textColor = UIColor.black
            if i%2 != 0{
                label.layer.masksToBounds = true
                label.layer.borderWidth = 1
                label.layer.borderColor = UIColor.black.cgColor
                label.text = "0"
            }
            self.addSubview(label)
            labelArrayMark.append(label )
        }
        labelArrayMark[0].text = "Head"
        labelArrayMark[2].text = "Tail"
        
        
        for j in 0...1{
            for i in 0...6{
                let label = UILabel()
                label.frame = CGRect(x: 20+CGFloat(i)*CGFloat(lableQueueWidth), y: 300+CGFloat(j)*2*CGFloat(lableQueueWidth), width: lableQueueWidth, height: lableQueueWidth)
                label.textAlignment = .center
                label.textColor = UIColor.black
                label.layer.masksToBounds = true
                label.layer.borderWidth = 0.5
                label.layer.borderColor = UIColor.black.cgColor
                self.addSubview(label)
                labelArrayQueue.append(label )
            }
        }
        
        for j in 0...1{
            for i in 0...6{
                let label = UILabel()
                label.frame = CGRect(x: 20+CGFloat(i)*CGFloat(lableQueueWidth), y: 300+CGFloat(lableQueueWidth)+CGFloat(j)*2*CGFloat(lableQueueWidth), width: lableQueueWidth, height: lableQueueWidth)
                label.textAlignment = .center
                label.text = String(i+j*7)
                label.textColor = UIColor.black
                self.addSubview(label)
                labelArraySubscriptMark.append(label)
            }
        }
        
        labelCircle.center = labelArrayMark[1].center
        labelCircle.isHidden = true
        labelCircle.frame = CGRect(x: labelArrayMark[1].frame.minX, y: labelArrayMark[1].frame.minY, width: lableQueueWidth, height: lableQueueWidth)
        labelCircle.layer.masksToBounds = true
        labelCircle.layer.cornerRadius = lableQueueWidth/2
        labelCircle.layer.borderWidth = 2
        labelCircle.layer.borderColor = UIColor.blue.cgColor
        self.addSubview(labelCircle)
        
    }
    //控制每一步动画总时间
    @objc func ActionSliderValueChange(){
        sumTime = Double(slider.value)
    }
    //    动画逻辑----------------------------------------------------------------
    //    Enqueue入队操作---------------------------------------------------------
    @objc func ActionButtonEnqueue(){
        //        输入框有内容且队列不为满 则执行入队操作
        if textField.text != "" && (tailOfTheQueue+1)%(labelArrayQueue.count) != headOfTheQueue {
            buttonIsEnabledFalse()
            enqueueOne()
        }
    }
    
    func enqueueOne(){
        labelValue.text = textField.text
        labelCircle.center = labelArrayMark[3].center
        UIView.animate(withDuration: sumTime, animations: {
            self.labelCircle.isHidden = false
            self.labelMark.isHidden = false
            self.labelValue.isHidden = false
            self.labelMark.text = "Enqueuing"
            self.labelValue.text = self.textField.text
            self.labelCircle.center = self.labelArraySubscriptMark[self.tailOfTheQueue].center
        }) { (_) in
            self.enqueueTwo()
        }
    }
    func enqueueTwo(){
        UIView.animate(withDuration: sumTime, animations: {
            self.labelValue.center = self.labelArrayQueue[self.tailOfTheQueue].center
            
        }) { (_) in
            self.labelValue.frame = CGRect(x: 130, y: 90, width: 80, height: 20)
            self.labelValue.isHidden = true
            self.labelCircle.isHidden = true
            self.labelArrayQueue[self.tailOfTheQueue].text = self.labelValue.text
            self.enqueueThree()
        }
    }
    func enqueueThree(){
        time = Timer.scheduledTimer(timeInterval: sumTime/10, target: self, selector: #selector(enqueueTime), userInfo: nil, repeats: true)
    }
    @objc func enqueueTime(){
        self.labelArrayMark[3].layer.borderColor = UIColor.red.cgColor
        self.labelArrayMark[3].layer.borderWidth += 1
        timeNo += 1
        if timeNo == 4{
            time.invalidate()
            self.labelArrayMark[3].layer.borderColor = UIColor.black.cgColor
            self.labelArrayMark[3].layer.borderWidth = 1
            self.tailOfTheQueue = (self.tailOfTheQueue+1)%(labelArrayQueue.count)//重要条件
            self.textField.text = ""
            self.labelMark.isHidden = true
            self.labelArrayMark[3].text = "\(tailOfTheQueue)"
            self.buttonIsEnabledTrue()
            timeNo = 0
        }
    }
    //    清除队列
    @objc func ActionClearQueue(){
        labelMark.isHidden = true
        labelValue.isHidden = true
        labelArrayMark[1].text = "0"
        labelArrayMark[3].text = "0"
        tailOfTheQueue = 0
        headOfTheQueue = 0
        for i in labelArrayQueue{
            i.text = ""
        }
    }
    //   出队操作----------------------------------------------------------------
    @objc func ActionDequeue(){
        
        if labelArrayQueue[headOfTheQueue].text != "" && (tailOfTheQueue)%labelArrayQueue.count != headOfTheQueue{
            dequeueOne()
            buttonIsEnabledFalse()
        }
        
        
    }
    func dequeueOne(){
        labelCircle.center = labelArrayMark[1].center
        UIView.animate(withDuration: sumTime, animations: {
            self.labelCircle.isHidden = false
            self.labelCircle.center = self.labelArraySubscriptMark[self.headOfTheQueue].center
        }) { (_) in
            self.labelCircle.isHidden = true
            self.dequeueTwo()
        }
    }
    func dequeueTwo(){
        self.labelMark.text = "Dequeued Value:"
        self.labelValue.center = self.labelArrayQueue[self.headOfTheQueue].center
        self.labelValue.text = self.labelArrayQueue[self.headOfTheQueue].text
        UIView.animate(withDuration: sumTime, animations: {
            self.labelArrayQueue[self.headOfTheQueue].text = ""
            self.labelValue.isHidden = false
            self.labelMark.isHidden = false
            self.labelValue.frame = CGRect(x: 130, y: 90, width: 80, height: 20)
        }) { (_) in
            self.dequeueThree()
        }
    }
    func dequeueThree(){
        time = Timer.scheduledTimer(timeInterval: sumTime/10, target: self, selector: #selector(dequeueTime), userInfo: nil, repeats: true)
    }
    @objc func dequeueTime(){
        self.labelArrayMark[1].layer.borderColor = UIColor.red.cgColor
        self.labelArrayMark[1].layer.borderWidth += 1
        timeNo += 1
        if timeNo == 4{
            time.invalidate()
            self.labelArrayMark[1].layer.borderColor = UIColor.black.cgColor
            self.labelArrayMark[1].layer.borderWidth = 1
            self.headOfTheQueue = (self.headOfTheQueue+1)%(labelArrayQueue.count)//重要条件
            self.textField.text = ""
            self.buttonIsEnabledTrue()
            self.labelArrayMark[1].text = "\(headOfTheQueue)"
            timeNo = 0
        }
    }
    
    //    控制按钮是否可点击
    func buttonIsEnabledFalse(){
        buttonDequeue.isEnabled = false
        buttonEnqueue.isEnabled = false
        buttonClearQueue.isEnabled = false
        slider.isEnabled = false
    }
    func buttonIsEnabledTrue(){
        buttonDequeue.isEnabled = true
        buttonEnqueue.isEnabled = true
        buttonClearQueue.isEnabled = true
        slider.isEnabled = true
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
